package cn.edu.gsau.info.controller;

import cn.edu.gsau.info.dao.LunwenDao;
import cn.edu.gsau.info.exception.BusinessException;
import cn.edu.gsau.info.models.*;
import cn.edu.gsau.info.service.ILunwenService;
import cn.edu.gsau.info.service.IShenheService;
import cn.edu.gsau.info.service.IUserService;
import cn.edu.gsau.info.service.IZhengmingcailiaoService;
import cn.edu.gsau.info.util.UploadUtil;
import cn.edu.gsau.info.vo.Paramex;
import cn.edu.gsau.info.vo.Paramss;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: gsau
 * @BelongsPackage: cn.edu.gsau.info.controller
 * @Author: yq
 * @CreateTime: 2022-11-19
 * @Description:  论文的相关操作
 * @Version: 1.0
 */
@RestController
@RequestMapping("/lunwen")
@JsonSerialize(using = ToStringSerializer.class)
public class LunwenController {
    @Autowired
    private ILunwenService iLunwenService;

    @Autowired
    LunwenDao lunwenDao;


    private static String path1;
    private static String path2;


    @Autowired
    private IZhengmingcailiaoService zhengmingcailiaoService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IShenheService shenheService;

    @Value("${gsau.upload.path}")
    private String uploadpath;

    @GetMapping("findShenhe/{recoreId}")
    public ResponseResult<Shenhe> findShenhe(@PathVariable Long recoreId){
        List<Shenhe> shenhes = iLunwenService.selectShenhe(recoreId);
        return new ResponseResult(Code.GET_OK,"查询成功",shenhes);
    }


    /**
     * @description:审核更新
     * @author: yq
     * @date:  2022-11-30
     * @param: 实体类
     * @return:
     **/
    @PutMapping("updateShenhe")
    public ResponseResult<Shenhe> updateShenhe(@RequestBody Shenhe shenhe){
        List<Shenhe> shenhes=iLunwenService.updateShenhe(shenhe);
        return new ResponseResult(Code.UPDATE_OK,"审核成功",shenhes);
    }

    /**
     * @description:
     * @author: yq
     * @date: 2022-11-29
     * @param:
     * @return: 集合
     **/
    @PostMapping("findexamine")
    public ResponseResult<Shenhe> findExamine(@RequestBody Paramex paramex){
        List<Lunwen> examines = iLunwenService.searchExamine(paramex);
        return new ResponseResult(Code.GET_OK,"查找成功",examines);
    }
    /**
     * @description: 证明材料删除
     * @author: yq
     * @date: 2022-11-26
     * @param: id
     * @return:
     **/
    @DeleteMapping("deletecailiao/{id}")
    public ResponseResult deletecailiao(@PathVariable Long id){
        boolean b = zhengmingcailiaoService.removeById(id);
        return new ResponseResult(Code.DELETE_OK,"删除成功");
    }

    /**
     * @description:
     * @author: yq
     * @date: 2022-11-26
     * @param: 论文实体
     * @return:
     **/
    @PutMapping("/update")
    public ResponseResult updateLunwen(@RequestBody Lunwen lunwen){

        System.out.println(lunwen.toString());
        List<Lunwen> lunwens = iLunwenService.updateLunwen(lunwen);
        System.out.println(lunwen.getManager());
        return new ResponseResult(Code.UPDATE_OK,"更新成功",lunwens);
    }

    /**
     * @description: 添加信息
     * @author: yq
     * @date:  2022-11-23
     * @param: lunwen实体类
     * @return: boolean
     **/
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Lunwen lunwen){
        List<Lunwen> lunwens = iLunwenService.addInfo(lunwen);
        return new ResponseResult(Code.SAVE_OK,"添加成功",lunwens);
    }

    /**
     * @description: 文件上传到本地 d://upload 下
     * @author: yq
     * @date:
     * @param: file
     * @return: map
     **/
    @PostMapping("/upload")
    public ResponseResult save(@RequestBody MultipartFile file){
        File path = new File(uploadpath);
        if(path.exists()==false){
            path.mkdirs();
        }
        User user=new User();
        user.setUserName("yangqiang");
        String originalFilename = file.getOriginalFilename();
        UploadUtil uploadUtil=new UploadUtil();
        Map<String,String > map  = uploadUtil.filedUpload(file, user.getUserName(), uploadpath);
        System.out.println(map);
        return new ResponseResult(Code.SAVE_OK,"保存成功",map);

    }

    /**
     * @description: 逻辑删除
     * @author: yq
     * @date:  2022-11-21
     * @param: id
     * @return:
     **/
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id){
        boolean b = iLunwenService.removeById(id);
        return new ResponseResult(Code.DELETE_OK,"删除成功");
    }

    /**
     * @description: 单条论文记录查询
     * @author: yq
     * @date: 2022-11-19
     * @param: id
     * @return:list集合
     **/
    @GetMapping("/find/{id}")
    public ResponseResult findById(@PathVariable long id){
        Lunwen lunwen=null;
        try{
           lunwen=iLunwenService.selectOne(id);

        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(Code.OTHER_EXCEPTION,"查询错误");
        }
        return new ResponseResult(Code.GET_OK,"查询成功",lunwen);
    }

    /**
     * @description: 查询论文
     * @author: yq
     * @date:  2022-11-19
     * @param:
     * @return: list集合
     **/
    @GetMapping("findall")
    public ResponseResult findAll(){
        User user=new User();
        user.setId(5L);
        List<Lunwen> lunwens =  iLunwenService.selectLunwen(user.getId());
        return new ResponseResult(Code.GET_OK,"查询成功",lunwens);
    }

    /**
     * @description:查询相关证明材料
     * @author: yq
     * @date:  2022-11-21
     * @param: id1：模板类型id，id2：某一条记录的id
     * @return: list集合
     **/
    @GetMapping("findzhengmingcailiao/{id2}")
    public ResponseResult findZhengMingCaiLiao(@PathVariable Long id2){
        List<Zhengmingcailiao> list=iLunwenService.findZhengMingCaiLiao(id2);
        return new ResponseResult(Code.GET_OK,"查询成功",list);
    }

    /**
     * @description: 通过状态查询论文
     * @author: yq
     * @date:  2022-11-24
     * @param:
     * @return: list集合
     **/
    @PostMapping("searchall")
    public ResponseResult search(@RequestBody Paramss params){
        List<Lunwen> lunwens = null;
        try{
            lunwens=iLunwenService.search(params);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(Code.OTHER_EXCEPTION,"搜索失败");
        }
        return new ResponseResult(Code.GET_OK,"搜索成功",lunwens);
    }

}

